<?php
/**
 * Tên file mình có thể tự chọn nhưng nên đặc theo têm controller sẽ dễ quản lý
 */

/**
 * Tên class: Example_Models_Dao_Pdo_Mysql_Example(Tenmodule_Models_Dao_Pdo_Mysql_Tenfile)
 */

class Enterprise_Models_Dao_Pdo_Mysql_Payment extends Tomato_Model_Dao
    implements Enterprise_Models_Interface_Payment
{
    public function convert($entity)
    {
        return new Enterprise_Models_Payment($entity);
    }

    public function add($payment){
        $this->_conn->insert($this->_prefix . 'enterprise_account_payment',
                            array(
                                'enterprise_id'             => $payment->enterprise_id,
                                'last_payment_date'         => $payment->last_payment_date,
                                'last_payment_amount'       => $payment->last_payment_amount,
                                'account_valid_until_date'  => $payment->account_valid_until_date,
                            ));
        return $this->_conn->lastInsertId($this->_prefix . 'enterprise_account_payment');
    }

    public function getById($id){
        $row = $this->_conn
                    ->select()
                    ->from(array('p' => $this->_prefix . 'enterprise_account_payment'))
                    ->where('p.enterprise_id = ?', $id)
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Enterprise_Models_Payment($row);
    }

    public function update($payment)
    {
        return $this->_conn->update($this->_prefix . 'enterprise_account_payment',
                                array(
                                    'last_payment_date'         => $payment->last_payment_date,
                                    'last_payment_amount'       => $payment->last_payment_amount,
                                    'account_valid_until_date'  => $payment->account_valid_until_date,
                                ),
                                array(
                                    'enterprise_id = ?' => $payment->enterprise_id,
                                ));
    }
}
